home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / windowsformsdemo / keyboardmouseform.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  12.1 KB  |  302 lines

  1. Public Class KeyboardMouseForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents txtNumber As System.Windows.Forms.TextBox
  26.     Friend WithEvents Label1 As System.Windows.Forms.Label
  27.     Friend WithEvents txtUpperCase As System.Windows.Forms.TextBox
  28.     Friend WithEvents Label2 As System.Windows.Forms.Label
  29.     Friend WithEvents Label3 As System.Windows.Forms.Label
  30.     Friend WithEvents txtDate As System.Windows.Forms.TextBox
  31.     Friend WithEvents lblInfo As System.Windows.Forms.Label
  32.     Friend WithEvents btnOK As System.Windows.Forms.Button
  33.     Friend WithEvents btnCancel As System.Windows.Forms.Button
  34.     Friend WithEvents chkCausesValidation As System.Windows.Forms.CheckBox
  35.  
  36.     'Required by the Windows Form Designer
  37.     Private components As System.ComponentModel.Container
  38.  
  39.     'NOTE: The following procedure is required by the Windows Form Designer
  40.     'It can be modified using the Windows Form Designer.  
  41.     'Do not modify it using the code editor.
  42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  43.         Me.Label3 = New System.Windows.Forms.Label()
  44.         Me.txtNumber = New System.Windows.Forms.TextBox()
  45.         Me.chkCausesValidation = New System.Windows.Forms.CheckBox()
  46.         Me.btnCancel = New System.Windows.Forms.Button()
  47.         Me.btnOK = New System.Windows.Forms.Button()
  48.         Me.lblInfo = New System.Windows.Forms.Label()
  49.         Me.txtDate = New System.Windows.Forms.TextBox()
  50.         Me.txtUpperCase = New System.Windows.Forms.TextBox()
  51.         Me.Label1 = New System.Windows.Forms.Label()
  52.         Me.Label2 = New System.Windows.Forms.Label()
  53.         Me.SuspendLayout()
  54.         '
  55.         'Label3
  56.         '
  57.         Me.Label3.Location = New System.Drawing.Point(16, 160)
  58.         Me.Label3.Name = "Label3"
  59.         Me.Label3.Size = New System.Drawing.Size(288, 16)
  60.         Me.Label3.TabIndex = 4
  61.         Me.Label3.Text = "Date field (press Shift-F2 for current date)"
  62.         '
  63.         'txtNumber
  64.         '
  65.         Me.txtNumber.Location = New System.Drawing.Point(16, 32)
  66.         Me.txtNumber.Name = "txtNumber"
  67.         Me.txtNumber.Size = New System.Drawing.Size(288, 24)
  68.         Me.txtNumber.TabIndex = 1
  69.         Me.txtNumber.Text = ""
  70.         '
  71.         'chkCausesValidation
  72.         '
  73.         Me.chkCausesValidation.CausesValidation = False
  74.         Me.chkCausesValidation.Checked = True
  75.         Me.chkCausesValidation.CheckState = System.Windows.Forms.CheckState.Checked
  76.         Me.chkCausesValidation.Location = New System.Drawing.Point(16, 128)
  77.         Me.chkCausesValidation.Name = "chkCausesValidation"
  78.         Me.chkCausesValidation.Size = New System.Drawing.Size(304, 16)
  79.         Me.chkCausesValidation.TabIndex = 8
  80.         Me.chkCausesValidation.Text = "CausesValidation property of 2nd textbox"
  81.         '
  82.         'btnCancel
  83.         '
  84.         Me.btnCancel.CausesValidation = False
  85.         Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
  86.         Me.btnCancel.Location = New System.Drawing.Point(328, 80)
  87.         Me.btnCancel.Name = "btnCancel"
  88.         Me.btnCancel.Size = New System.Drawing.Size(72, 32)
  89.         Me.btnCancel.TabIndex = 7
  90.         Me.btnCancel.Text = "Cancel"
  91.         '
  92.         'btnOK
  93.         '
  94.         Me.btnOK.Location = New System.Drawing.Point(328, 32)
  95.         Me.btnOK.Name = "btnOK"
  96.         Me.btnOK.Size = New System.Drawing.Size(72, 32)
  97.         Me.btnOK.TabIndex = 7
  98.         Me.btnOK.Text = "OK"
  99.         '
  100.         'lblInfo
  101.         '
  102.         Me.lblInfo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  103.         Me.lblInfo.Dock = System.Windows.Forms.DockStyle.Bottom
  104.         Me.lblInfo.Location = New System.Drawing.Point(0, 229)
  105.         Me.lblInfo.Name = "lblInfo"
  106.         Me.lblInfo.Size = New System.Drawing.Size(424, 24)
  107.         Me.lblInfo.TabIndex = 6
  108.         '
  109.         'txtDate
  110.         '
  111.         Me.txtDate.Location = New System.Drawing.Point(16, 184)
  112.         Me.txtDate.Name = "txtDate"
  113.         Me.txtDate.Size = New System.Drawing.Size(288, 24)
  114.         Me.txtDate.TabIndex = 5
  115.         Me.txtDate.Text = ""
  116.         '
  117.         'txtUpperCase
  118.         '
  119.         Me.txtUpperCase.Location = New System.Drawing.Point(16, 96)
  120.         Me.txtUpperCase.Name = "txtUpperCase"
  121.         Me.txtUpperCase.Size = New System.Drawing.Size(288, 24)
  122.         Me.txtUpperCase.TabIndex = 3
  123.         Me.txtUpperCase.Text = ""
  124.         '
  125.         'Label1
  126.         '
  127.         Me.Label1.Location = New System.Drawing.Point(16, 8)
  128.         Me.Label1.Name = "Label1"
  129.         Me.Label1.Size = New System.Drawing.Size(296, 16)
  130.         Me.Label1.TabIndex = 0
  131.         Me.Label1.Text = "Numeric value (must be non-empty)"
  132.         '
  133.         'Label2
  134.         '
  135.         Me.Label2.Location = New System.Drawing.Point(16, 72)
  136.         Me.Label2.Name = "Label2"
  137.         Me.Label2.Size = New System.Drawing.Size(176, 16)
  138.         Me.Label2.TabIndex = 2
  139.         Me.Label2.Text = "Uppercase string"
  140.         '
  141.         'KeyboardMouseForm
  142.         '
  143.         Me.AcceptButton = Me.btnOK
  144.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  145.         Me.CancelButton = Me.btnCancel
  146.         Me.ClientSize = New System.Drawing.Size(424, 253)
  147.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkCausesValidation, Me.btnCancel, Me.btnOK, Me.lblInfo, Me.Label3, Me.txtDate, Me.txtUpperCase, Me.Label2, Me.Label1, Me.txtNumber})
  148.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  149.         Me.HelpButton = True
  150.         Me.KeyPreview = True
  151.         Me.Name = "KeyboardMouseForm"
  152.         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  153.         Me.Text = "Keyboard and Mouse"
  154.         Me.ResumeLayout(False)
  155.  
  156.     End Sub
  157.  
  158. #End Region
  159.  
  160.     Private Sub KeyboardMouseForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  161.         InitializeMouseEvents(Me)
  162.     End Sub
  163.  
  164.     ' a KeyPress event handler that ignores anything that isn't
  165.     ' a digit key or a control character 
  166.  
  167.     Private Sub txtNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
  168.         If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
  169.             e.Handled = True
  170.         End If
  171.     End Sub
  172.  
  173.     ' a KeyPress handler that converts the char to uppercase 
  174.  
  175.     Private Sub txtUpperCase_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUpperCase.KeyPress
  176.         ' Replace the selected text with an uppercase character
  177.         txtUpperCase.SelectedText = e.KeyChar.ToString.ToUpper
  178.         ' Cancel standard processing
  179.         e.Handled = True
  180.     End Sub
  181.  
  182.     ' this KeyDown event handler replaces the selection with current
  183.     'date if the user presses the Shift-F2 key
  184.  
  185.     Private Sub txtDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDate.KeyDown
  186.         If e.Shift And (e.KeyCode = Keys.F2) Then
  187.             txtDate.SelectedText = Today.Date.ToString
  188.             e.Handled = True
  189.         End If
  190.     End Sub
  191.  
  192.     ' form-level key handler
  193.  
  194.     Private Sub KeyboardMouseForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  195.         Dim t As String = ""
  196.         ' build the status text
  197.         If e.Alt Then t = "Alt "
  198.         If e.Control Then t &= "Control "
  199.         If e.Shift Then t &= "Shift "
  200.         If t.Length <> 0 Then t = "(" & t.Trim & ") "
  201.  
  202.         t &= String.Format("KeyCode={0} KeyData={1} KeyValue={2}", e.KeyCode, e.KeyData, e.KeyValue)
  203.         lblInfo.Text = t
  204.  
  205.         If Me.Controls.Count = 0 Then
  206.             ' Ignore forms without any control.
  207.         ElseIf e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
  208.             ' Start at the currently selected control.
  209.             Dim ctrl As Control = Me.ActiveControl
  210.             ' Decide the direction once and for all.
  211.             Dim moveForward As Boolean = (e.KeyCode = Keys.Down)
  212.  
  213.             Do
  214.                 ' Get the next control in that direction.
  215.                 ctrl = Me.GetNextControl(ctrl, moveForward)
  216.                 ' GetNectControl(ctrl,False) can return Nothing if first control.
  217.                 If Not (ctrl Is Nothing) AndAlso ctrl.CanFocus AndAlso ctrl.TabStop Then
  218.                     ' If the control can receive the focus, give it.
  219.                     ctrl.Focus()
  220.                     Exit Do
  221.                 End If
  222.             Loop
  223.         End If
  224.  
  225.     End Sub
  226.  
  227.     ' display a message box when F1 is pressed inside txtDate field
  228.  
  229.     Private Sub txtDate_HelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles txtDate.HelpRequested
  230.         MessageBox.Show("Insert a date", "Help Requested", MessageBoxButtons.OK, MessageBoxIcon.Information)
  231.     End Sub
  232.  
  233.     ' display mouse information when it is moved or clicked on the form
  234.  
  235.     Private Sub Form_MouseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown, MyBase.MouseUp, MyBase.MouseMove, MyBase.MouseWheel
  236.         lblInfo.Text = String.Format("Mouse: Buttons={0}  X={1} Y={2} Clicks={3} Delta={4}", e.Button, e.X, e.Y, e.Clicks, e.Delta)
  237.     End Sub
  238.  
  239.     ' change and restore background color when the mouse
  240.     ' enter/exits a control
  241.  
  242.     Private Sub MouseEnterEvent(ByVal sender As Object, ByVal e As System.EventArgs)
  243.         DirectCast(sender, Control).BackColor = Color.Yellow
  244.     End Sub
  245.  
  246.     Private Sub MouseLeaveEvent(ByVal sender As Object, ByVal e As System.EventArgs)
  247.         DirectCast(sender, Control).BackColor = Color.White
  248.     End Sub
  249.  
  250.     ' initialize the mouse events for all textbox and combobox 
  251.     ' controls on the form
  252.  
  253.     Sub InitializeMouseEvents(ByVal frm As Windows.Forms.Form)
  254.         Dim ctrl As Control
  255.         For Each ctrl In frm.Controls
  256.             If (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is ComboBox) Then
  257.                 AddHandler ctrl.MouseEnter, AddressOf MouseEnterEvent
  258.                 AddHandler ctrl.MouseLeave, AddressOf MouseLeaveEvent
  259.             End If
  260.         Next
  261.     End Sub
  262.  
  263.     ' This event proves a bug with the validation schema
  264.  
  265.     Private Sub txtNumber_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtNumber.Validating
  266.         ' don't validate if the form is closing
  267.         If isClosing Then Exit Sub
  268.  
  269.         If txtNumber.Text = "" Then
  270.             e.Cancel = True
  271.             Beep()
  272.             lblInfo.Text = "First Textbox is required"
  273.         Else
  274.             lblInfo.Text = "First TextBox's Validation event has fired"
  275.         End If
  276.     End Sub
  277.  
  278.     ' close the form when OK and Cancel buttons are clicked 
  279.  
  280.     Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
  281.         CloseForm()
  282.     End Sub
  283.  
  284.     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  285.         CloseForm()
  286.     End Sub
  287.  
  288.     Private Sub chkCausesValidation_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCausesValidation.CheckedChanged
  289.         txtUpperCase.CausesValidation = chkCausesValidation.Checked
  290.     End Sub
  291.  
  292.     Dim isClosing As Boolean
  293.  
  294.     ' this procedure closes the form. It sets the closing variable
  295.     ' so that other validation routines don't prevent the closing
  296.     Sub CloseForm()
  297.         isClosing = True
  298.         Me.Close()
  299.     End Sub
  300.  
  301. End Class
  302.